
package main;

import managers.InputManager;

import java.awt.*;
import java.awt.event.*;

/**
 * This is just a simple test example for the input manager snippet. It is
 * just a simple frame and a threaded loop that runs about every 16 milliseconds
 * or so which is 62.5 times per second.
 */
public class InputManagerTest extends Frame implements Runnable {

	// The test input manager
	private final InputManager input;

	// Used to only start the looping thread once.
	private boolean				isTestLoopRunning	= false;

	/**
	 * Add the InputManager as the KeyListener to the Frame.
	 */
	public InputManagerTest() {
		input = InputManager.getInstance();
		this.addKeyListener(input);

		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				startTestLoop();
			}
		});
	}

	/**
	 * Starts a new thread using this class as the Runnable instance for that
	 * thread.
	 */
	public void startTestLoop() {
		//only want to run once.
		if (!isTestLoopRunning) {
			(new Thread(this)).start();
			isTestLoopRunning = true;
		}
	}

	/**
	 * The meat of the test to check and make sure the input manager is working
	 * correctly.
	 */
	@Override
	public void run() {
		System.out.println("Start Test Loop");

		//use this to test how many times the loop detects a key is down
		int count = 0;

		//run while the frame is showing
		while (this.isShowing()) {

			// run test cases
			if (input.isAnyKeyDown()) {
				System.out.println("Some key is down");
			}

			if (input.isAnyKeyUp()) {
				System.out.println("Some key is up");
			}

			if(input.isKeyDown(KeyEvent.VK_SPACE)) {
				++count;
				System.out.println("Spacebar is down");
			}

			if(input.isKeyUp(KeyEvent.VK_SPACE)) {
				System.out.println("Spacebar is up");
				System.out.format("Spacebar detected down %d times\n", count);
				count = 0; //reset the counter
			}

			input.update();

			try {
				Thread.sleep(16);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // just do a constant 16 milliseconds
		}
		System.out.println("End Test Loop");
	}
	
	/**
	 * Main method to use to run the test from, it creates a basic awt frame and
	 * displays the frame.
	 * 
	 * @param args Command line arguments.
	 */
	public static void main(String[] args) {
		final Frame frame = new InputManagerTest(); //create our test class.
		
		// set a default size for the window, basically just give it some area.
		frame.setSize(640, 480);
		
		frame.setLocationRelativeTo(null); // centers window on screen
		
		// Need to add this to handle window closing events with the awt Frame.
		frame.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent arg0) {
				frame.setVisible(false);
				frame.dispose();
				System.exit(0);
			}

		});
		
		// show the frame
		frame.setVisible(true);
	}
	
}
